from Crypto.Cipher import AES
from Crypto.Random import random
from CA import *

def pad(msg):
    n = 16 - len(msg) % 16
    return msg + bytes([n]) * n

def unpad(msg):
    assert len(msg) > 0 and len(msg) % 16 == 0
    n = msg[-1]
    assert 1 <= n <= 16
    assert msg[-n:] == bytes([n]) * n
    return msg[:-n]

def aes_dec(ci,ke):
    key=int.to_bytes(ke%(2**128),16,'big')
    bcmess=bytes.fromhex(ci)
    baes_iv,bcipher=bcmess[:16],bcmess[16:]
    baes=AES.new(key,AES.MODE_CBC,baes_iv)
    bmess=unpad(baes.decrypt(bcipher)).decode()
    return bmess

def aes_enc(me,ke):
    aes_key=int.to_bytes(ke%(2**128),16,'big')
    aes_iv = b"\x00"*16
    aes=AES.new(aes_key, AES.MODE_CBC, aes_iv)
    cmess=(aes_iv+aes.encrypt(pad(me.encode()))).hex()
    return cmess

#KEY="e71533dfc4d2aa22f70f30325525a93036752a4e6393167ea4696fdcefbdae6be929434025bb040c1d6a3241a1e1e505d4ca8e5ee1e170b18760a8be51641e9cddaeaf87c5a1abd60611c4d7a6561bf1f747ac037f7fdbc137286837852931a74d34ecaef52e81b6950962fe7bfc015bf7d06a494d194b8e1727ff3366fcb7f"
KEY="1"
upk=int(KEY,16)


# aes_key=1
# aes_c="7fe273d82652a93f049d187d3855790be92990139ee7dbc983bcd2836ff739e24de5e499c9193311fa5c5624ac1b8fdc925cc45dc6308aa0dbb9a543b937a76998e5f2e084e7e58223e5221748d42b0e6eb845feadfd2b182ac8764a2a89e0fbe927ab60ded17ffa2929c701008b7c5c64dfae71eda2e3d004251a7c7df2465a34dd9d77a1d1d5dfc3f3459c884ca36e5633b9b62d800c354a18ec47a5551ec9487ce26933a4374fccc0d301bb9cba77aea354ef0240e6544da59360e06ef90ed52e83747f7db027ef6384703c549c464feff10d0eaf77995dfd73b670fb9a53e48a5ff65ba1e1629c3e04ee4bd794496cd3c1f0c6b749fcb9adf9d3b7b42a3bc2d2984d634f2a469a68cad0b3a11c1006ad4a3a20f7766f96bde11ae2e96d6b"
# print(aes_dec(aes_c,aes_key))
a_cert="416c696365_7d2e831fc7c37b4932604c66de52a3c7f631eee0badffb42f40899fb88ff19e41518fc8326f620aaeb54cecd8bd22a5fa8576848c131370c0fcb8fb0e093fbd0d76b73c47c04ffe2b913472595c9987d53b8cdffa4ff16f6a6176f215e6ded63e65089d763691a4cef91284e6934b95580e64854304b99e87215b582e201222d"

# aes_key=1
# aes_c="c81e1d4bfb93e7bcae0c2244618609b965ee696311e57d7387c8cf197c23a79f91941e56ddbe33b3288761b19eaf74c39155593d9f57bee003079e65db2018978137cef60f53659ac4de482e6b5731072bdf6d92ff47cfc2a3d975a0b8c76ccc5cec191ba068267e37ed4f76e116a19a1277372f83db4c20a8113367d05df22e8786861fa25d8a827abce8d117919c4212ebfacc70e987c7bdda5c71809a0aa0028ac1b749911c1390c7230730955b99ec7ed95d45613d33594a95a2a9e3a6dd08f4d17f9b5d332aac3c5b3127c5b366c46c78a8696ffcb36d7943c52d8f01c29d60823aedeae1efb9cbb0fe32124858edd58365be7ed7969b8d344b0a5c42c4c135babe71875dfb7bed62448fa584d307890991fec58228ed9a951d531b5228"
# print(aes_dec(aes_c,aes_key))
b_cert="426f62_2453e551e17519b6dcfdae46f75b9d92dd943fcb5d2c71911adb83f43753dfba86be013dfb8c2413cbc70eac852e312040d3fbf471b57da2ae7a7aa95d39f3b972e34214f5cc69093d9929b2c5fc24684b0d5443dcb8338d1f52bb34f9913bd1e98c29302bbbd3cb532001adde7f06d93badcdee7b12b1b550a816537e950bad"

flag1c="5d3719c665e4fc4538b8475fe8988f376b1c3768a41933cf93e42859ac9ee88f3b6a1cabd05a83e6a4a09aeab5f4ff5137afd138941c597beb27f35c20d813c6be6a9f0e678b9e7df1bb6095f08cb87c43a33c6bc525bade554e7f65c3df150aa65b5332fde1ae32fe124a48f940c64fa43a1d5994ce5869f2acabb50b9fd72808c9b807c13cf797b8121e89a12a3577dd5b183d2b31396c8ece8192a0fa855dbe452b06d2318de74ce974beb39764870338c3002a1a9796f8354ffd54b6ce830a0f2efde9525a18ed307e23b9ed52541f0621365ee4e679655c33fe3a416368600e589b24429651fb959dbbd928e7ace54b99db09a78fe372c356ca05b490377c97ba351095b797615199926f41499e453cfcaf127ff6320768ef5de211842c"
#print(aes_dec(flag1c,1))



d=int("18dc7cf25fcccab2ca37427aa2b2f0c6554c5e67928b2e22f9fd7813df01b9c3106c27bdc87f1ce980d639ddef13ae97ada67e9f04acbd25424c8754ad373a3c419be41f0a5e6b62abf93e6d0211cf42428cf370f11e0b77fd16fdcc02dd9f12e400f84d3b96fd28db9d183e4aa494539756ed50094a5ec2d6d7d098b7ac0e89",16)
pk=pow(G,d,P)
print(hex(pk)[2:])

key=pow(upk,d,P)

print(aes_enc(b_cert,key))
flag2c="8ad1aacc07433ead5dbff23736840568b84d1302f9904fe41bb2e06bbcdb36a4acaf1934cad56797f459ecada267007dcde26ff44fe3fa3a4e48cd5945e49216"
flag2c="a310a35d89fb951bbe69448155bb4dcdb1d66b21d6ddd75fdf879f565dfb0175e58cb0ae1806867ed21a7482c72453227adb208e377ffc5466454929b0d92624"
print(aes_dec(flag2c,key))